package org.itboys.mobile.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.SundayProductRelated;
import org.itboys.mobile.service.mongo.web.SundayProductRelatedService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：zzq
 * 时间：2018/8/15
 * 描述：
 */
@Controller
@RequestMapping("/sunday/web/product/related")
public class SundayProductRelatedController extends BaseController{
    @Autowired
    private SundayProductRelatedService relatedService;
    @RequestMapping("/index" )
    public  String index(HttpServletRequest request,
                         @RequestParam(value="productId",required=true)Long productId,
                         Model model){
        model.addAttribute("productId",productId);
        return "/sunday/product/related/index";
    }
    @RequestMapping("/input")
    public String input(
            HttpServletRequest request,
            @RequestParam(value="id",required=true)Long id,
            @RequestParam(value="productId",required=true)Long productId,
            Model model
    ){
        SundayProductRelated related=relatedService.findOne(id);
        model.addAttribute("related",related);
        model.addAttribute("productId",productId);
        return "/sunday/product/related/input";
    }

    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="productId",required=true)Long productId
    ) {
        try {
            PageResult<SundayProductRelated> result=relatedService.select(request,productId);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    @RequestMapping("/save")
    public  void save(HttpServletRequest request,
                      HttpServletResponse response,
                      @RequestParam(value="productId",required=true)Long productId,
                      @RequestParam(value="otherId",required=true)Long otherId,
                      @RequestParam(value="otherName",required=true)String otherName,
                      @RequestParam(value="otherImage",required=true)String otherImage,
                      @RequestParam(value="type",required=true)Integer type
    ) {
        try {
            relatedService.save(productId ,otherId,otherName,otherImage, type);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/product/related/save------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));;
        }
    }
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="id",required=true) Long id

    ) {
        try {
            relatedService.delete(id);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {

            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/product/related/delete------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
}
